package com.brillilab.service.core.inventory.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.inventory.InventoryRecordMapper;
import com.brillilab.dao.mapper.kit.KitMapper;
import com.brillilab.domain.enums.inventory.InventoryRecordEnum;
import com.brillilab.domain.po.inventory.InventoryRecord;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryRecordService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
@Transactional
public class InventoryRecordServiceImpl implements IInventoryRecordService{

	@Resource
	private InventoryRecordMapper inventoryRecordMapper;
	@Resource
	private KitMapper kitMapper;
	

	@Override
	public InventoryRecord insert(InventoryRecord entity) {
		Integer insert = inventoryRecordMapper.insert(entity);
		Assert.isTrue(insert==1, "添加失败");
		Kit kit = new Kit();
		kit.setId(entity.getKitId());
		kit.setUpdateTime(new Date());
		kitMapper.updateById(kit);
		return entity;
	}
	
	@Override
	public boolean deleteById(Long id) {
		Integer b = inventoryRecordMapper.deleteById(id);
		return b==1 ? true: false;
	}
	@Override
	public boolean updateById(InventoryRecord InventoryRecord) {
		Integer b = inventoryRecordMapper.updateById(InventoryRecord);
		return b==1 ? true: false;
	}
	
	@Override
	public InventoryRecord selectById(Long id) {
		return inventoryRecordMapper.selectById(id);
	}

	@Override
	public PageVo<InventoryRecord> selectList(Long kitId, int pageNum, int pageSize) {
		LambdaQueryWrapper<InventoryRecord> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(InventoryRecord::getKitId, kitId);
		wrapper.ne(InventoryRecord::getOperate, 2);
		wrapper.orderByDesc(InventoryRecord::getCreateTime);
		IPage<InventoryRecord> page = new Page<>(pageNum, pageSize);
		IPage<InventoryRecord> selectPage = inventoryRecordMapper.selectPage(page, wrapper);
		return new PageVo<>(selectPage);
	}

	@Override
	public InventoryRecord add(UserInfoVo urs, Long kitId, InventoryRecordEnum operate) {
		InventoryRecord entity = new InventoryRecord();
		entity.setKitId(kitId);
		entity.setLabId(urs.getLabId());
		entity.setUserId(urs.getId());
		entity.setUserName(urs.getName());
		entity.setOperate(operate.getValue());
		entity.setOperateName(operate.getKey());
		return this.insert(entity);
	}

	@Override
	public void add(UserInfoVo user, List<Long> kitIds, InventoryRecordEnum operate) {
		for (Iterator<Long> iterator = kitIds.iterator(); iterator.hasNext();) {
			Long kitId = iterator.next();
			if(kitId==null) {
				iterator.remove();
			}
		}
		if(!CollectionUtils.isEmpty(kitIds)) {
			inventoryRecordMapper.insertBatch(kitIds,user,operate);
			
			Kit kit = new Kit();
			kit.setUpdateTime(new Date());
			LambdaQueryWrapper<Kit> wrapper = new LambdaQueryWrapper<>();
			wrapper.in(Kit::getId, kitIds);
			kitMapper.update(kit, wrapper);
		}
	}

}
